/*
 *  Copyright 2019-2023 oyun
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oyun.modules.document.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oyun.annotation.Log;
import com.oyun.modules.document.domain.DocPicture;
import com.oyun.modules.document.domain.vo.DocPictureQueryCriteria;
import com.oyun.modules.document.service.DocPictureService;
import com.oyun.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author oyun
 * @date 2024-05-13
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "文献图片管理管理")
@RequestMapping("/api/docPicture")
public class DocPictureController {

    private final DocPictureService docPictureService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('docPicture:list')")
    public void exportDocPicture(HttpServletResponse response, DocPictureQueryCriteria criteria) throws IOException {
        docPictureService.download(docPictureService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询文献图片管理")
    @ApiOperation("查询文献图片管理")
    @PreAuthorize("@el.check('docPicture:list')")
    public ResponseEntity<PageResult<DocPicture>> queryDocPicture(DocPictureQueryCriteria criteria, Page<Object> page) {
        return new ResponseEntity<>(docPictureService.queryAll(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增文献图片管理")
    @ApiOperation("新增文献图片管理")
    @PreAuthorize("@el.check('docPicture:add')")
    public ResponseEntity<Object> createDocPicture(@Validated @RequestBody DocPicture resources) {
        docPictureService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改文献图片管理")
    @ApiOperation("修改文献图片管理")
    @PreAuthorize("@el.check('docPicture:edit')")
    public ResponseEntity<Object> updateDocPicture(@Validated @RequestBody DocPicture resources) {
        docPictureService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除文献图片管理")
    @ApiOperation("删除文献图片管理")
    @PreAuthorize("@el.check('docPicture:del')")
    public ResponseEntity<Object> deleteDocPicture(@RequestBody List<Long> ids) {
        docPictureService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping("getPictureList/{docId}")
    @ApiOperation("根据文献id获取图片")
    public ResponseEntity<Object> getPictureList(@PathVariable("docId") Long docId) {
        return new ResponseEntity<>(docPictureService.getPictureList(docId), HttpStatus.OK);
    }

    @GetMapping("getPictureListById/{docId}")
    @ApiOperation("根据文献id获取图片")
    public ResponseEntity<Object> getPictureListById(@PathVariable("docId") Long docId) {
        return new ResponseEntity<>(docPictureService.getPictureList(docId), HttpStatus.OK);
    }

    @GetMapping("getFirstPictureById/{docId}")
    @ApiOperation("根据文献id获取第一张图片")
    public ResponseEntity<Object> getFirstPictureById(@PathVariable("docId") Long docId) {
        return new ResponseEntity<>(docPictureService.getFirstPictureById(docId), HttpStatus.OK);
    }

    @GetMapping("getNextPicture/{docId}/{pageNum}")
    @ApiOperation("根据文献id获取下一个图片")
    public ResponseEntity<Object> getNextPicture(@PathVariable("docId") Long docId, @PathVariable("pageNum") Long pageNum) {
        return new ResponseEntity<>(docPictureService.getNextPicture(docId, pageNum), HttpStatus.OK);
    }

    @GetMapping("getPrePicture/{docId}/{pageNum}")
    @ApiOperation("根据文献id获取上一个图片")
    public ResponseEntity<Object> getPrePicture(@PathVariable("docId") Long docId, @PathVariable("pageNum") Long pageNum) {
        return new ResponseEntity<>(docPictureService.getPrePicture(docId, pageNum), HttpStatus.OK);
    }

    @GetMapping("skip/{docId}/{pageNum}")
    @ApiOperation("根据文献id跳转")
    public ResponseEntity<Object> skip(@PathVariable("docId") Long docId, @PathVariable("pageNum") Long pageNum) {
        return new ResponseEntity<>(docPictureService.skip(docId, pageNum), HttpStatus.OK);
    }

    @GetMapping("totality/{docId}")
    @ApiOperation("根据文献id跳转")
    public ResponseEntity<Object> skip(@PathVariable("docId") Long docId) {
        return new ResponseEntity<>(docPictureService.totality(docId), HttpStatus.OK);
    }
}
